package com.web44.ssm.advice;

import com.web44.ssm.utils.ResponseVo;
import com.web44.ssm.utils.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * @Title: PojectException
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2024/1/15 15:22
 */
// @RestControllerAdvice
@ControllerAdvice
public class PojectException {

    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(Exception es, HttpServletResponse response,HttpServletRequest request) throws IOException {
        boolean isjosn = (boolean)request.getAttribute("isjosn");
        if(isjosn) {
            ServletUtils.writeResponse(response, ResponseVo.error(es.toString()));
        }else {
            // 页面的请求
            // 跳转的方式
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("error",es.toString());
            modelAndView.setViewName("error");
            return modelAndView;
        }
        return null;

    }
}
